package com.chenjim.glrecorder;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    CameraGlView cameraGlView;
    private long startTime = 0;
    private boolean flag = false;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraGlView = findViewById(R.id.cameraView);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Button recordButton = findViewById(R.id.btn_record);
        recordButton.setOnTouchListener((View v, MotionEvent event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    /*if (flag) {
                        cameraGlView.stopRecord();
                        Log.d("ddd", "结束");
                    } else {
                        cameraGlView.startRecord();
                        startTime = SystemClock.elapsedRealtime();
                        Log.d("ddd", "开始");
                    }
                    flag = !flag;*/
                    cameraGlView.saveFile();
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    if (SystemClock.elapsedRealtime() - startTime < 2000) {
                        // recordButton.postDelayed(() -> cameraGlView.stopRecord(), 1500);
                    } else {
                        //cameraGlView.stopRecord();
                    }
                    break;
                default:
                    break;
            }
            return true;
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraGlView.stopRecord();
    }
}
